/*! ******************************************************************************
 *
 * Created on 2022年3月28日
 *
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.yzbdl.lanius.core.metastore;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.persistence.Column;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;

/**
 * @author hs@yzbdl.ac.cn
 *
 */
public class DbCookies {

    public static String getFields(Class<? extends BaseEntity> entityClass, String alias, String prefix) {

        StringBuffer sqlFragment = new StringBuffer();

        Field[] fields = ReflectUtil.getFields(entityClass);
        for (Field field : fields) {
            Column column = field.getAnnotation(Column.class);
            if (column != null && StrUtil.isNotBlank(column.name())) {
                String name = column.name();
                sqlFragment.append(" ");
                sqlFragment.append(alias).append(".").append(name);
                sqlFragment.append(" as ");
                sqlFragment.append(prefix + name).append(",");
            }
        }

        if (sqlFragment.length() > 0) {
            sqlFragment.deleteCharAt(sqlFragment.length() - 1);
        }

        return sqlFragment.toString();
    }

    public static void fillField(Entity map, BaseEntity entity, String prefix) {
        PropertyDescriptor[] pds = BeanUtil.getPropertyDescriptors(entity.getClass());
        for (PropertyDescriptor pd : pds) {
            try {
                Field field = ReflectUtil.getField(entity.getClass(), pd.getName());
                if (field == null) {
                    continue;
                }
                Column column = field.getAnnotation(Column.class);
                if (column != null && StrUtil.isNotBlank(column.name())) {
                    String dbField = column.name();
                    Class<?> clazz = pd.getPropertyType();
                    Object args = getArgs(map, clazz, prefix + dbField);
                    Method setter = pd.getWriteMethod();
                    setter.invoke(entity, args);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static Object getArgs(Entity map, Class<?> clazz, String argsName) {
        String className = clazz.getName();
        switch (className) {
            case "java.lang.Integer":
                return map.getInt(argsName);
            case "java.lang.Long":
                return map.getLong(argsName);
            case "java.lang.String":
                return map.getStr(argsName);
            case "java.lang.Boolean":
                return map.getBool(argsName);
            case "java.util.Date":
                return map.getDate(argsName);
            default:
                return null;
        }
    }

}
